package com.sawen.houserent.controller;

import com.alibaba.fastjson.JSONObject;
import com.sawen.houserent.bean.Stu;
import com.sawen.houserent.bean.User;
import com.sawen.houserent.service.UserService;
import com.sawen.houserent.util.RandomPwd;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping(value = "/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private StringRedisTemplate redisTemplate;


    /**注册*/
    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public void insertSelective(User user){
        userService.insertSelective(user);
        System.out.println(user.getEmail());
        rabbitTemplate.convertAndSend("exchange","Topic.regist",user);
       /**注册成功自动登陆*/
        autoLogin(user);
    }

    /**登陆*/
    @RequestMapping(value = "/login",method =RequestMethod.POST)
    public User login(User user){
        User user1= userService.login(user);
        String key = "user";
        ValueOperations opts = redisTemplate.opsForValue();
        boolean hasKey = redisTemplate.hasKey(key);
        if(hasKey){
            String users =(String) opts.get(key);
            return JSONObject.parseObject(users,User.class);
        }
        String userStr = JSONObject.toJSONString(user1);
        opts.set(key,userStr,100, TimeUnit.SECONDS);
        rabbitTemplate.convertAndSend("exchange","Topic.login",user1);
        return user1;
    }

    /**获取随机密码*/
    @RequestMapping(value = "/getRandomPwd",method = RequestMethod.GET)
    public void getRandomPwd(){
        String pwd=RandomPwd.randomPassword();
        String key = "pwd";
        ValueOperations opts = redisTemplate.opsForValue();
        System.out.println(pwd);
        opts.set(key,pwd,200,TimeUnit.SECONDS);
        rabbitTemplate.convertAndSend("exchange","Topic.rpl",pwd);

    }

    /**随机密码登陆*/
    @RequestMapping(value  ="/loginByRandomPwd",method = RequestMethod.POST)
    public void loginByRandomPwd(User user){
        ValueOperations opts = redisTemplate.opsForValue();
        String key = "pwd";
        String upwd = user.getLoginpwd();
        Object randompwd= opts.get(key);
        System.out.println(upwd+"------g-----"+randompwd);
        if(randompwd.equals(upwd)){
            User user1=userService.getEmail(user);
            System.out.println(user1);
            if(user1!=null){
                String key1 = "user_"+user1;
                String userStr = JSONObject.toJSONString(user1);
                opts.set(key1,userStr,60,TimeUnit.SECONDS);
                rabbitTemplate.convertAndSend("exchange","Topic.rpl",user1);
            }
        }
    }

    /**自动登录*/
    @RequestMapping(value = "/autoLogin",method = RequestMethod.POST)
    public void autoLogin(User user){
        userService.autoLogin(user);
        User user1=userService.setlctUser(user);
        String key = "user";
        ValueOperations opts = redisTemplate.opsForValue();
        String userStr = JSONObject.toJSONString(user1);
        opts.set(key,userStr,100, TimeUnit.SECONDS);
    }
}
